package database;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import object.SalaryBonus;
import objectView.SBCompany;
import utility.StringUtil;
import clientMain.Login;
import clientMain.Main;

public class SBDelegate {
	
	public Vector<SBCompany> getListSB(int page, String sort, boolean flag){
		Vector<SBCompany> list = new Vector<SBCompany>();
		try {
			Login.conn.setAutoCommit(false);
			CallableStatement cst = Login.conn.prepareCall("{call sp_ListSB(?,?,?,?,?)}");
			cst.setInt(1, Main.comId);
			cst.setInt(2, Main.record);
			cst.setInt(3, page);
			cst.setString(4, sort);
			cst.setBoolean(5, flag);
			ResultSet rs = cst.executeQuery();
			while(rs.next()){
				SBCompany sb = new SBCompany();
				sb.setsBId(rs.getInt("SB_Id"));
				sb.setsB_Name(StringUtil.getFilterIllegal(rs.getString("SB_Name")));
				sb.setsBDescribe(StringUtil.getFilterIllegal(rs.getString("SB_Describe")));
				sb.setsBMoney(Integer.toString(rs.getInt("SB_Money")));
				sb.setDepName(StringUtil.getFilterIllegal(rs.getString("Dep_Name")));
				list.add(sb);
			}
			Login.conn.commit();
		} catch (SQLException e) {
			list = null;
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * @author Tidus
	 * @param emId
	 * @category list SB of EmId 
	 * @return
	 */
	public Vector<SalaryBonus> listEMSB(String emId){
		Vector<SalaryBonus> list = new Vector<SalaryBonus>();
		try {
			Login.conn.setAutoCommit(false);
			CallableStatement cst = Login.conn.prepareCall("{call sp_ListSBEm(?)}");
			cst.setString(1, emId);
			ResultSet rs = cst.executeQuery();
			while(rs.next()){
				SalaryBonus sb = new SalaryBonus();
				sb.setsBId(rs.getInt(1));
				sb.setsB_Name(StringUtil.getFilterIllegal(rs.getString(2)));
				list.add(sb);
			}
			Login.conn.commit();
		} catch (SQLException e) {
			e.printStackTrace();
			list = null;
		}
		return list;
	}
}
